Ext.define('AmpleHarvest.objects.importrequesttool.view.ImportRequestGrid',
{
   extend         	: 'Ext.grid.Panel',
   alias			: 'widget.ampleharvest-objects-importrequesttool-view-importrequestgrid',
   requires   	 	:
   [
         "AmpleHarvest.objects.importrequesttool.store.ImportRequestList",
         "Ext.grid.column.Action", 
         "Ext.toolbar.Paging"
   ],
   viewConfig     :
   {
      forceFit : true
   },
   frame       : true,
   listeners       :
   {
	   reloadData : function()
	   {
	   		this.refreshData();
	   }
   },

   constructor    : function(config)
   {
   	  this.str_CurrentUser = obj_config.user.userName;
   	
      this.bool_EditOwnS1 = config.data.HR_EDIT_OWN_STAGE1;
      this.bool_EditOwnS2 = config.data.HR_EDIT_OWN_STAGE2;
      this.bool_EditAllS1 = config.data.HR_EDIT_ALL_STAGE1;
      this.bool_EditAllS2 = config.data.HR_EDIT_ALL_STAGE2;
      this.bool_DeactivateAll = config.data.HR_REQUEST_DEACTIVATE_ALL;
      this.bool_DeactivateOwn = config.data.HR_REQUEST_DEACTIVATE_OWN;
      this.bool_ViewAll = config.data.HR_VIEW_ALL;
   	
      var obj_This = this;

      this.m_int_PageSize = 30;

      this.store = Ext.create("AmpleHarvest.objects.importrequesttool.store.ImportRequestList",
            {
               pageSize : this.m_int_PageSize
            });

      this.m_obj_Pageing = Ext.create('Ext.toolbar.Paging',
            {
               displayInfo : true,
               emptyMsg : 'No Import Requests Found',
               store    : this.store
            });

      this.bbar = this.m_obj_Pageing;

      this.callParent();
   },

   initComponent  : function()
   {

      this.columns =
      [
            {
               xtype       : 'actioncolumn',
               width       : 90,
               fixed       : true,
               menuDisabled   : true,
               items       :
               [
                     {
                        tooltip     : 'Deactivate Import',
                        scope    : this,
                        getClass : function(v, meta, rec)
                        { // Or return a class from a function
                           if (rec.get("UserName") == this.str_CurrentUser)
                           {
                              if (this.bool_DeactivateOwn)
                              {
                                 return "harvestRequest-delete action-icon";
                              }
                           }
                           if (this.bool_DeactivateAll)
                           {
                              return "harvestRequest-delete action-icon";
                           }
                           return "action-icon";
                        },
                        handler     : function(grid, rowIndex, colIndex)
                        {
                           var rec = this.store.getAt(rowIndex);
                           m_obj_AmpleHarvest_ViewPort.fireEvent(AmpleHarvest.eventHandler.cWindowEvents.C_STR_EVENT_HR_DEACTIVATE, rec.data.id);
                        }
                     },
                     {
                        tooltip     : 'Edit Import',
                        scope    : this,
                        getClass : function(v, meta, rec)
                        { // Or return a class from a function
                           if (rec.get("UserName") == this.str_CurrentUser)
                           {
                              if (this.bool_EditOwnS1)
                              {
                                 return "harvestRequest-edit action-icon";
                              }
                           }
                           if (this.bool_EditAllS1)
                           {
                              return "harvestRequest-edit action-icon";
                           }
                           return "action-icon";
                        },
                        handler     : function(grid, rowIndex, colIndex)
                        {
                           var rec = this.store.getAt(rowIndex);
                           m_obj_AmpleHarvest_ViewPort.fireEvent(
                        	   AmpleHarvest.eventHandler.cWindowEvents.C_STR_EVENT_LOAD_IMPORT_REQUEST,
                        	   rec.data.id, rec.data.user_id, true, 'Edit Import Request');
                        }
                     },
                     {
                        tooltip     : 'View Import',
                        scope    : this,
                        getClass : function(v, meta, rec)
                        { // Or return a class from a function
                           if (rec.get("UserName") == this.str_CurrentUser)
                           {
                              if (this.bool_ViewAll)
                              {
                                 return "harvestRequest-view action-icon";
                              }
                           }
                           if (this.bool_ViewAll)
                           {
                              return "harvestRequest-view action-icon";
                           }
                           return "action-icon";
                        },
                        handler     : function(grid, rowIndex, colIndex)
                        {
                           var rec = this.store.getAt(rowIndex);
                           m_obj_AmpleHarvest_ViewPort.fireEvent(
                        	   AmpleHarvest.eventHandler.cWindowEvents.C_STR_EVENT_LOAD_IMPORT_REQUEST,
                        	   rec.data.id, rec.data.user_id, false, 'View Import Request');
                        }
                     },                     
                     {
                        tooltip     : 'Set Importer',
                        scope    : this,
                        getClass : function(v, meta, rec)
                        { // Or return a class from a function
                           if (rec.get("UserName") == this.str_CurrentUser)
                           {
                              if (this.bool_ViewAll)
                              {
                                 return "menu-admin-profile-icon action-icon";
                              }
                           }
                           if (this.bool_ViewAll)
                           {
                              return "menu-admin-profile-icon action-icon";
                           }
                           return "action-icon";
                        },
                        handler     : function(grid, rowIndex, colIndex)
                        {
                        	var rec = this.store.getAt(rowIndex);
                            var obj_Window =  Ext.create('AmpleHarvest.objects.usermanagerplugins.usersbygroup.main',
                                                   {
                                                            m_str_group_ID : 'F1D8A48E-884C-40B1-8572-40658ACE062A',
                                                            m_str_rec_ID	: rec.data.id,
                                                            AHCaller		: this
                                                   });

                           m_obj_AmpleHarvest_ViewPort.add(obj_Window);
                           obj_Window.show();
                        }
                     }
               ]
            },
            {
               header      : "Request #",
               sortable : true,
               dataIndex   : 'requestNum',
               width    : 75
            },
            {
               header      : "Source URL",
               sortable : true,
               dataIndex   : 'url',
               flex     : 5
            },
            {
                header      : "Time Sensitive",
                sortable : true,
                dataIndex   : 'urgency',
                flex     : 2
             },
            {
               header      : 'Date Requested',
               sortable : true,
               dataIndex   : 'date_requested',
               width    : 150
            },
            {
               header      : "Requester",
               sortable : true,
               dataIndex   : 'user_sid',
               flex     : 1
            },
            {
               header      : "Status",
               sortable : true,
               dataIndex   : 'StatusLabel',
               flex     : 1
            },
            {
                header      : "Importer",
                sortable : true,
                dataIndex   : 'importer_sid',
                flex     : 1
             }
      ]
      this.callParent();
      this.refreshData();
   },

   set_Action     : function(str_Action)
   {
      this.store.set_Action(str_Action);
   },
   
   set_Param     : function(strParam, strValue)
   {
	  this.store.set_Action(str_Action);
   },
   
   getStore    :function()
   {
	  return this.store;
   },

   refreshData    : function()
   {
      this.store.load();
   }
}
);